﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using RetrieveEntityDetails.CrmDiscoveryServiceProxy;


namespace RetrieveEntityDetails.Utility
{
    class MetaDataServiceHelper
    {
        MetadataService oMetadataService;
        public string Username;
        public string Password;
        public string Domain;
        public string ServerName;
        public string PortNumber;
        public string PassportTicket;
        public string OrganizationName;
        public bool IsIFD = false;
        public bool IsHTTPS = false;
        public bool IsCRMOnline = false;
        CrmAuthenticationToken token;

        public MetaDataServiceHelper()
        {

        }

        public CrmMetadata[] GetEntityList()
        {
            oMetadataService = new MetadataService();

            if (PortNumber != null || PortNumber == string.Empty)
                oMetadataService.Url = "http://" + ServerName + "/MSCRMServices/2007/MetadataService.asmx";
            else
                oMetadataService.Url = "http://" + ServerName + ":" + PortNumber + "/MSCRMServices/2007/MetadataService.asmx";
            if (IsIFD)
                oMetadataService.Url = oMetadataService.Url.Replace("/AD/", "/SPLA/");
            if (IsHTTPS)
                oMetadataService.Url = oMetadataService.Url.Replace("http", "https");
            if (IsCRMOnline)
            {
                CrmDiscoveryService oCrmDiscoveryService = new CrmDiscoveryService();
                oCrmDiscoveryService.Url = "https://dev.crm.dynamics.com/MSCRMServices/2007/PASSPORT/CrmDiscoveryService.asmx";
                RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
                ticketRequest.OrganizationName = OrganizationName;
                ticketRequest.PassportTicket = PassportTicket;
                RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)oCrmDiscoveryService.Execute(ticketRequest);
                token = new CrmAuthenticationToken();
                token.AuthenticationType = 1;
                token.OrganizationName = OrganizationName;
                token.CrmTicket = ticketResponse.CrmTicket;
            }
            else
            {
                if (Username == null & Password == null & Domain == null)
                {
                    oMetadataService.UseDefaultCredentials = true;
                }
                else
                {
                    oMetadataService.UseDefaultCredentials = false;
                    oMetadataService.Credentials = new NetworkCredential(Username, Password, Domain);
                }
                if (IsIFD)
                {
                    CrmDiscoveryService oCrmDiscoveryService = new CrmDiscoveryService();
                    if (!string.IsNullOrEmpty(PortNumber))
                        oCrmDiscoveryService.Url = "http://" + ServerName + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";
                    else
                        oCrmDiscoveryService.Url = "http://" + ServerName + ":" + PortNumber + "/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";
                    if (IsIFD)
                        oCrmDiscoveryService.Url = oCrmDiscoveryService.Url.Replace("/AD/", "/SPLA/");
                    if (IsHTTPS)
                        oCrmDiscoveryService.Url = oCrmDiscoveryService.Url.Replace("http", "https");
                    RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
                    ticketRequest.OrganizationName = OrganizationName;
                    ticketRequest.UserId = Domain + "\\" + Username;
                    ticketRequest.Password = Password;
                    RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)oCrmDiscoveryService.Execute(ticketRequest);
                    token = new CrmAuthenticationToken();
                    token.AuthenticationType = 2;
                    token.OrganizationName = OrganizationName;
                    token.CrmTicket = ticketResponse.CrmTicket;
                }
                else
                {
                    token = new CrmAuthenticationToken();
                    token.OrganizationName = OrganizationName;
                    token.AuthenticationType = AuthenticationType.AD;
                }
            }
            oMetadataService.CrmAuthenticationTokenValue = token;
            RetrieveAllEntitiesRequest oRequest = new RetrieveAllEntitiesRequest();
            oRequest.MetadataItems = MetadataItems.IncludeAttributes;
            oRequest.RetrieveAsIfPublished = true;
            RetrieveAllEntitiesResponse oResponse = (RetrieveAllEntitiesResponse)oMetadataService.Execute(oRequest);
            return oResponse.CrmMetadata;
        }
    }
}
